package Test.jsp.noconn;

import java.io.File;
import java.io.FileWriter;

import Test.tools.Utils;


public class JSPAddTemp {

	public static void jSPAddTest(String tableName,String tableBean,String currentBao) throws  Exception{
			String rn = "\r\n";
			String []array = Utils.stringToArray(tableBean);
			StringBuilder sb = new StringBuilder();
			for (int i = 1; i < array.length; i++) {
				String bean = 
					"	 <tr><td>"+ array[i]+"：" + rn + 
					"	 <input type=\"text\" name=\""+ array[i]+"\"></td></tr>" + rn ;
				sb.append(bean);
			}
			
			StringBuilder sbcheck = new StringBuilder();
			for (int i = 1; i < array.length; i++) {
				String beancheck = 
					"	   var "+array[i]+"=document.getElementsByName(\""+array[i]+"\")[0];" + rn + 
					"	   if("+array[i]+".value ==null || "+array[i]+".value  == \"\"){" + rn + 
					"	       alert(\""+array[i]+"不能为空……\");" + rn + 
					"	       "+array[i]+".value =\"\";" + rn + 
					"	       "+array[i]+".focus();" + rn + 
					"	       return false;" + rn +
				    "	    }" + rn;
				sbcheck.append(beancheck);
			}
			
			String src = 
			
			
			"<%@ page language=\"java\"  import=\"java.util.*\" pageEncoding=\"UTF-8\"%>" + rn + 
			"<%@ taglib uri=\"http://java.sun.com/jsp/jstl/core\" prefix=\"c\"%>" + rn + 
			"<%@ taglib prefix=\"form\" uri=\"http://www.springframework.org/tags/form\" %>" + rn + 
			"<%@ taglib prefix=\"fmt\" uri=\"http://java.sun.com/jsp/jstl/fmt\" %>" + rn + 
			"<%@ taglib prefix=\"fn\" uri=\"http://java.sun.com/jsp/jstl/functions\" %>" + rn + 
			"<%" + rn + 
			"String path = request.getContextPath();" + rn + 
			"String basePath = request.getScheme()+\"://\"+request.getServerName()+\":\"+request.getServerPort()+path+\"/\";" + rn + 
			"%>" + rn + 

			"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">" + rn + 
			"<html>" + rn + 
			"  <head>" + rn + 
			"    <base href=\"<%=basePath%>\">" + rn + 
			    
			"    <title>创建"+tableName+"</title>" + rn + 
			"    <script language=\"javascript\">" + rn + 
			
			"    function add(){" + rn + 
			sbcheck +
			"	   document.forms[0].submit();" + rn + 
			"	}" + rn + 
				
			"    function goback(){" + rn + 
			"       location.href=\""+tableName+"View.do\";" + rn + 
			"    }" + rn + 
			     
			"   </script>" + rn + 
			"  </head>" + rn + 
			"  <body>" + rn + 
			"  <form action=\""+tableName+"Add.do\" method=\"post\">  " + rn + 
			"  <table bgcolor=\"#CCFFFF\" border =\"0\" bordercolor=\"#969696\"  width=\"100%\" cellpadding=\"3\" cellspacing=\"0\">" + rn + 
			"	<tr bgcolor=\"#99CCFF\"><td colspan=\"100%\">"+tableName+"Add</td></tr>" + rn + 
			"    <tr><td colspan=\"100%\"><hr size=\"1\" color=\"#9999FF\"></hr></td></tr>" + rn + 
			
			sb +
			
			"	 <tr><td colspan=\"100%\"><hr size=\"1\" color=\"#9999FF\"></hr></td></tr>" + rn + 
			"	 <tr height=\"50\">" + rn + 
			"	<td colspan=\"100%\" align=\"right\">" + rn + 
			"	<input type=\"button\" onclick=\"add()\" value=\"add\" style=\"background:#CCFFFF\"/>&nbsp;&nbsp;&nbsp;" + rn + 
			"    <input type=\"button\" onclick=\"goback()\" value=\"goback\" style=\"background:#CCFFFF\"/>&nbsp;&nbsp;&nbsp;&nbsp;" + rn + 
			"	</td></tr>" + rn + 
			" </table>" + rn + 
			"  </form>" + rn + 
			"  </body>" + rn + 
			"</html>" + rn + 
			
			rn;
			
			String path = System.getProperty("user.dir")+"/WebRoot/back/jsp/"+tableName+"/";
			String fileName = System.getProperty("user.dir")+"/WebRoot/back/jsp/"+tableName+"/"+tableName+"Add.jsp";
			File fpath = new File(path);
			if (!fpath.exists()) {
			   fpath.mkdirs();
		    }
			File f = new File(fileName);
			FileWriter fw = new FileWriter(f);
			fw.write(src);
			fw.flush();
			fw.close();
	 }		
}
